package com.rxkj.utils;

import com.rxkj.entity.sys.UserInfo;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {

    private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DateFormat md = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private static long exptime = 7 * 24 * 60 * 60 * 1000; //毫秒

    private static long alexexptime = 24 * 60 * 60 * 1000; //毫秒,最低有效时间，小于它就重新更新有效时间

    /**
     * .withIssuer("dtodmart-deliver") //jwt签发者
     * .withSubject("rxkj.com") //面向的用户(jwt所面向的用户)
     * .withIssuedAt(nowtime) //jwt的签发时间
     * .withExpiresAt(JWTDateUtils.getexpdate(iatstr)); //过期时间戳(jwt的过期时间，这个过期时间必须要大于签发时间)
     * @param ui
     * @return
     */
    public static String getToken(UserInfo ui){
        Map<String, Object> headerClaims = new HashMap<>();
        Date nowtime = new Date();
        String iatstr = nowtime.getTime() + ""; //签发时间
        Date expdate = getexpdate(iatstr);
        String token="";
        token = JWT.create()
                .withIssuer("shidis") //jwt签发者
                .withSubject("rxkj.com") //面向的用户(jwt所面向的用户)
                //claim 相当于存储所需要交流的数据信息
                .withClaim("id", ui.getId())
                .withClaim("userName", ui.getUserName())
                .withClaim("roleLevel", ui.getRoleLevel())
                .withIssuedAt(nowtime) //jwt的签发时间
                .withExpiresAt(expdate) //过期时间戳(jwt的过期时间
                .withAudience(ui.getId().toString()) //接收jwt的一方
                .sign(Algorithm.HMAC256(ui.getUserPwd()));//生成签名
        return token;
    }

    public static String date_to_str(Date date){
        if (date != null){
            String str = "";
            str = df.format(date);
            return str;
        }
        return "";
    }

    public static String date_to_strx(Date date){
        if (date != null){
            String str = "";
            str = md.format(date);
            return str;
        }
        return "";
    }

    /**
     * 有效期设置暂定
     * @return
     */
    public static Date getexpdate(String timestr){
        //一般网上的转换是没有中间new Long（timeStamp）,因为他们都是精确到毫秒的时间戳，不用再乘以1000进行转换
        long longTimeStamp = new Long(timestr);
        Date date = new Date(longTimeStamp + exptime);
        return date;
    }

    private static long date_subtract_date(Date now, Date before){
        long nowtime = now.getTime();
        long beforetime = before.getTime();
        long exp = beforetime - nowtime;
        return exp;
    }

    public static boolean isoutexptime(String token){
        DecodedJWT decode = JWT.decode(token);
        //到期时间
        Date expiresAt = decode.getExpiresAt();
        //目前时间
        Date nowdate = new Date();
        long exptimestamp = date_subtract_date(nowdate, expiresAt);
        return exptimestamp <= 0;
    }

    /**
     * 判断是否快到有效期
     * @param token
     * @return
     */
    public static boolean isinexptime(String token){
        DecodedJWT decode = JWT.decode(token);
        //到期时间
        Date expiresAt = decode.getExpiresAt();
        //目前时间
        Date nowdate = new Date();
        long exptimestamp = date_subtract_date(nowdate, expiresAt);
        return exptimestamp < alexexptime;
    }

}
